package com.atguigu.zk;

import org.apache.zookeeper.*;
import org.apache.zookeeper.data.Stat;
import org.junit.Before;
import org.junit.Test;

import java.util.List;

/**
 *
 * zk操作客户端
 * @author : zjj
 * @date : 2021/7/2 9:01
 */
public class ZkClient {

    /**
     * zookeeper集群地址
     */
    private  String connectString = "hadoop_112:2181,hadoop_113:2182,hadoop_114:2181";
    /**
     * session过期时间
     */
    private Integer sessionTimeout = 2000;
    /**
     * zkClient
     */
    private ZooKeeper zkClient;

    @Before
    public void init()throws Exception{
        zkClient = new ZooKeeper(connectString, sessionTimeout, new Watcher() {
            public void process(WatchedEvent watchedEvent) {

            }
        });
    }

    @Test
    public void create() throws Exception{
        String nodeCreated = zkClient.create("/atguigu","ss.avi".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
    }

    @Test
    public void getChildren()throws Exception{

        List<String> children = zkClient.getChildren("/", true);
        System.out.println("==================获取所有的节点");
        for (String child : children){
            System.out.println(child);
        }

    }


    @Test
    public void exist()throws Exception{
        Stat exists = zkClient.exists("/atguigu", false);

        System.out.println(exists == null ? "not exist" : "exist");
    }




}
